package com.wenyu.wenyuyuedu.adapter.homepagefragment;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.bean.homepager.Homepage_All_item;
import com.wenyu.wenyuyuedu.utils.TimeStyle;
import com.wenyu.wenyuyuedu.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/2/22.
 */

public class HomeAllAdapter extends BaseAdapter {
    private Context context;
    private List<Homepage_All_item.InfoBean> infoList = new ArrayList<>();
    private Homepage_All_item.InfoBean infoBean;
    private HomepageAllHolder holder;
    private GridView grd;

    public HomeAllAdapter(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        return infoList.size();
    }

    @Override
    public Object getItem(int i) {
        return infoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        HomepageAllHolder holder = null;
        infoBean = infoList.get(i);
        if (view == null) {
            holder = new HomepageAllHolder();
            view = View.inflate(context, R.layout.homepage_all_item, null);
            holder.head_view = (CircleImageView) view.findViewById(R.id.home_all_head);
            holder.deityPaster = (ImageView) view.findViewById(R.id.home_all_shen);
            holder.nickName = (TextView) view.findViewById(R.id.hoeme_all_nickname);
            holder.releaseTime = (TextView) view.findViewById(R.id.home_all_releasetime);
            holder.classIfy = (TextView) view.findViewById(R.id.home_all_classify);
            holder.content = (TextView) view.findViewById(R.id.home_all_content);
            holder.contentOne = (ImageView) view.findViewById(R.id.home_all_content_picture_one);
            holder.contentTwo = (ImageView) view.findViewById(R.id.home_all_content_picture_two);
            holder.contentThree = (ImageView) view.findViewById(R.id.home_all_content_picture_three);
            holder.contentFour = (ImageView) view.findViewById(R.id.home_all_content_picture_four);
            holder.contentFive = (ImageView) view.findViewById(R.id.home_all_content_picture_five);
            holder.pictureCount = (TextView) view.findViewById(R.id.home_all_picture_count);
            holder.message = (ImageView) view.findViewById(R.id.home_all_message);
            holder.messageCount = (TextView) view.findViewById(R.id.home_all_messagecount);
            holder.zan = (ImageView) view.findViewById(R.id.home_all_zan);
            holder.zanCount = (TextView) view.findViewById(R.id.home_all_zancount);
            holder.playTour = (ImageView) view.findViewById(R.id.home_all_playtour);
            holder.playTourCount = (TextView) view.findViewById(R.id.home_all_playtourcount);
            holder.iv_home_all_transparency = (ImageView) view.findViewById(R.id.iv_home_all_transparency);
            holder.grd = (GridView) view.findViewById(R.id.grd);
            view.setTag(holder);
        } else {
            holder = (HomepageAllHolder) view.getTag();
        }
        this.holder = holder;
         /*
        * 点赞的数量
        * 点赞超过5个为神贴
        * */
        String support_num = infoBean.getSupport_num();
        int zanCount = Integer.parseInt(support_num);
        if (zanCount >= 5) {
            holder.playTour.setVisibility(View.VISIBLE);
            holder.playTourCount.setVisibility(View.VISIBLE);
            holder.deityPaster.setVisibility(View.VISIBLE);
            holder.playTourCount.setText(infoBean.getReward_num());
        } else {
            holder.playTour.setVisibility(View.GONE);
            holder.playTourCount.setVisibility(View.GONE);
            holder.deityPaster.setVisibility(View.GONE);
        }
        holder.zanCount.setText(infoBean.getSupport_num());
        /*
        * 头像
        * */
        Glide.with(context)
                .load(infoBean.getPath())
                .error(R.drawable.noamal_head_photo)
                .into(holder.head_view);

        /*
        * 名字
        * */
        holder.nickName.setText(infoBean.getNickname());
        /*
        * 圈子名字
        * */
        holder.classIfy.setText(infoBean.getTitle());
        /*
        * 发布时间
        * */
        String create_time = infoBean.getCreate_time();
        String s = TimeStyle.publishTime(create_time);
        holder.releaseTime.setText(s);
        /*
        * 发布的内容
        * */
        String content = infoBean.getContent();
        if(content.equals("")){
            holder.content.setVisibility(View.GONE);
        }else {
            holder.content.setText(infoBean.getContent());
            holder.content.setVisibility(View.VISIBLE);
        }
        /*
        * 评论数量
        * */
        holder.messageCount.setText(infoBean.getReply_num());
        /*
        * 图片的数量及展示
        * */
        ArrayList<ImageView> imageViews = new ArrayList<>();
        imageViews.add(holder.contentOne);
        imageViews.add(holder.contentTwo);
        imageViews.add(holder.contentThree);
        imageViews.add(holder.contentFour);
        imageViews.add(holder.contentFive);
        List<String> images = infoBean.getImages();
        holder.pictureCount.setVisibility(View.GONE);
        holder.iv_home_all_transparency.setVisibility(View.GONE);
        // 全部隐藏
        for (ImageView imageView : imageViews) {
            imageView.setVisibility(View.GONE);
        }
        if (images != null && images.size() >= 1) {
            // 根据个数显示
            for (int i1 = 0; i1 < images.size(); i1++) {
                if (i1 < 5) {
                    imageViews.get(i1).setVisibility(View.VISIBLE);
                    Glide.with(context)
                            .load(infoBean.getImages().get(i1))
                            .error(R.drawable.nomal_dynamic)
                            .into(imageViews.get(i1));
                }else {
                        holder.iv_home_all_transparency.setVisibility(View.VISIBLE);
                        holder.pictureCount.setVisibility(View.VISIBLE);
                        holder.pictureCount.setText("+" + String.valueOf(images.size() - 5));
                    break;
                }
            }
        }
        return view;
    }

    class HomepageAllHolder {
        CircleImageView head_view;
        ImageView deityPaster;
        TextView nickName;
        TextView releaseTime;
        TextView classIfy;
        TextView content;
        ImageView contentOne;
        ImageView contentTwo;
        ImageView contentThree;
        ImageView contentFour;
        ImageView contentFive;
        TextView pictureCount;
        ImageView message;
        TextView messageCount;
        ImageView zan;
        TextView zanCount;
        ImageView playTour;
        TextView playTourCount;
        ImageView iv_home_all_transparency;
        GridView grd;
    }

    public void addAll(List<Homepage_All_item.InfoBean> infoList) {
        if (infoList != null) {
            this.infoList = infoList;
            notifyDataSetChanged();
        }
    }
}
